//
//  BHGuideView.m
//  Behing
//
//  Created by Lj on 2017/11/6.
//  Copyright © 2017年 lj. All rights reserved.
//

#import "BHGuideView.h"

@interface BHGuideView ()<UIScrollViewDelegate>

@property (strong, nonatomic) UIScrollView *scrollView;
@property (strong, nonatomic) UIPageControl *pageControl;
@property (strong, nonatomic) UIButton *endGuideBtn;

@end

@implementation BHGuideView

+ (void)showGuideViewWithImages:(NSArray *)images {
    if ([BHGuideView doHaveShowGuideView]) {
        return;
    }
    BHGuideView *guideView = [[BHGuideView alloc] initWithFrame:CGRectMake(0, 0, App_Frame_Width, App_Frame_Height) images:images];
    [[UIApplication sharedApplication].keyWindow addSubview:guideView];
}

- (id)initWithFrame:(CGRect)frame images:(NSArray *)images {
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor whiteColor];
        
        _scrollView = [[UIScrollView alloc] initWithFrame:self.bounds];
        _scrollView.showsVerticalScrollIndicator = NO;
        _scrollView.showsHorizontalScrollIndicator = NO;
        _scrollView.pagingEnabled = YES;
        _scrollView.delegate = self;
        [self addSubview:_scrollView];
        _scrollView.contentSize = CGSizeMake(App_Frame_Width * images.count, App_Frame_Height);
        
        _pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(0, App_Frame_Height - 70, App_Frame_Width, 30)];
        _pageControl.currentPage = 0;
        _pageControl.pageIndicatorTintColor = [UIColor lightGrayColor];
        _pageControl.currentPageIndicatorTintColor = [UIColor colorWithHexString:@"#33B7A5"];
        //        [self addSubview:_pageControl];
        
        
        for (int i = 0; i < images.count; i ++) {
            UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(App_Frame_Width * i, 0, App_Frame_Width, App_Frame_Height)];
            imageView.contentMode = UIViewContentModeScaleAspectFit;
            imageView.image = [self imagePath:images[i]];
            [_scrollView addSubview:imageView];
            
            if (i == images.count-1) {
                imageView.userInteractionEnabled = YES;
                [imageView addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(endGuide)]];
            }
        }
        
        //        _endGuideBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        //        _endGuideBtn
    }
    return self;
}
- (void)endGuide {
    [UIView animateWithDuration:.3 animations:^{
        self.alpha = 0;
        self.transform = CGAffineTransformMakeScale(1.3, 1.3);
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
        
        //        NSDictionary *versionDic = @{@"version":[GuideView appVersion],@"doHaveShow":@(YES)};
        NSDictionary *versionDic = @{@"doHaveShow":@(YES)};
        [[NSUserDefaults standardUserDefaults] setObject:versionDic forKey:@"Guide_Version_Mess"];
        [[NSUserDefaults standardUserDefaults] synchronize];
    }];
}
- (UIImage *)imagePath:(NSString *)imageName {
    NSArray *arr = [imageName componentsSeparatedByString:@"."];
    NSString *imagePath = @"";
    if (arr.count == 2) {
        imagePath = [[NSBundle mainBundle] pathForResource:arr[0] ofType:arr[1]];
    }else {
        imagePath = [[NSBundle mainBundle] pathForResource:imageName ofType:@"jpg"];
        if (!imagePath) {
            imagePath = [[NSBundle mainBundle] pathForResource:imageName ofType:@"png"];
        }
    }
    //    imagePath = [[NSBundle mainBundle] pathForResource:imageName ofType:nil];
    if (imagePath == nil) {
        return [UIImage imageNamed:imageName];
    }else {
        return [UIImage imageWithContentsOfFile:imagePath];
    }
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    if (scrollView.contentOffset.x + App_Frame_Width > scrollView.contentSize.width + 20) {
        [self endGuide];
    }
}

+ (BOOL)doHaveShowGuideView {
    NSDictionary *dic = [[NSUserDefaults standardUserDefaults] valueForKey:@"Guide_Version_Mess"];
    if (dic) {
        //[dic[@"version"] isEqualToString:[GuideView appVersion]] &&
        if ([dic[@"doHaveShow"] boolValue]) {
            return YES;
        }else {
            return NO;
        }
    }else {
        return NO;
    }
    
}
+ (NSString *)appVersion {
    NSDictionary *infoDictionary = [[NSBundle mainBundle] infoDictionary];
    // app版本
    NSString *app_Version = [infoDictionary objectForKey:@"CFBundleShortVersionString"];
    // app build版本
    NSString *app_build = [infoDictionary objectForKey:@"CFBundleVersion"];
    
    return [app_Version stringByAppendingString:app_build];
}

@end
